package com.zt.springcloud.service.Impl;

import cloud.zt.springboot.constant.Constants;
import cloud.zt.springboot.exception.BaseException;
import cloud.zt.springboot.exception.Exp.CaptchaException;
import cloud.zt.springboot.exception.Exp.UserException;
import cloud.zt.springboot.utils.IdUtils;
import cloud.zt.springboot.utils.ResultUtils;
import cloud.zt.springboot.utils.StringUtils;
import com.zt.springcloud.RemoteUserService;
import com.zt.springcloud.entity.Address;
import com.zt.springcloud.entity.User;
import com.zt.springcloud.service.RedisService;
import com.zt.springcloud.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author RedReins
 * @version 1.0
 * @description: TODO
 * @date 2021/9/2 23:19
 */

@Component
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {
    Logger logger = LoggerFactory.getLogger("UserServiceImpl");

    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RedisService redisService;




    @Override
    public User getLoginUser(String username) {
        logger.info("用户名：{}",username);
        User user = remoteUserService.getUserInfo(username);
        if(StringUtils.isNull(user))
            throw new BaseException("用户名未找到");
        return user;
    }

    @Override
    public ResultUtils<?> checkUser(String username, String password, String code, String uuid) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        String captcha = (String) redisService.get(verifyKey);

        if (StringUtils.isNull(username)||StringUtils.isNull(password)) {
            throw new CaptchaException("账号或者密码为空");
        }
        if (StringUtils.isNull(captcha)||!code.equalsIgnoreCase(captcha)) {
            throw new CaptchaException("验证码错误");
        }
        User userInfo = null;
        try{
            userInfo = remoteUserService.getUserInfo(username);
        }catch (Exception e){
            throw new CaptchaException("获取用户失败");
        }
        logger.info(userInfo.getPassword()+":"+password);
        if(userInfo.getPassword().equals(password)){
            return ResultUtils.fail("账号或者密码错误");
        }
        /*生成token*/
        String token = IdUtils.simpleUUID();
        redisService.set(Constants.LOGIN_TOKEN_KEY+token, username);
        return ResultUtils.success(Constants.TOKEN_PREFIX+token);
    }

    @Override
    public ResultUtils<?> doLogout(String token) {
        if(!StringUtils.isNull(token)){
            redisService.del(token);
        }else{
            throw new UserException("token无效");
        }
        return ResultUtils.success("用户注销成功");
    }

    @Override
    public Integer addUserAddress(Address address) {
        return remoteUserService.addUserAddress(address);
    }

    @Override
    public List<Address> getAddressInfo(Long userid) {
        return remoteUserService.getAddressInfo(userid);
    }

    @Override
    public Integer updateAddressInfo(Address address) {
        return remoteUserService.updateAddressInfo(address);
    }
}
